<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 应用公共文件

//获取redis的链接

function getRedis(){
    $config=getConfig();
    $redis=new Redis($config);
    return $redis;
}
//获取redis配置
function getConfig(){
    $config=[
        'host'=>'172.16.0.16',
        'port'=>6379,
        'password'=>'',
        'select'=>0,
        'timeout'=>0,
        'expire'=>0,
        'persistent'=>false,
        'prefix'=>'',
    ];
    return $config;
}

//返回消息提示
function renderInfo($errcode,$errmsg){
    header('Content-type: application/json');
    $data=array();
    $data['errcode']=$errcode;
    $data['errmsg']=$errmsg;
    echo json_encode($data);
    exit;
}

//带有数据的返回提示

function renderData($errcode,$errmsg,$subData){
    header('Content-type: application/json');
    $data['errcode']=$errcode;
    $data['errmsg']=$errmsg;
    $data['data']=$subData;
    echo json_encode($data);
    exit;
}

//验证userId对应的token是否一致。0:检查家长账号 1:检查孩子token
function checkToken($userId,$token){
    if(empty($userId)||empty($token)){
        renderInfo(400007,'验证失败');
    }
    $redis=getRedis();
    $info=$redis->get(config('app.user_flag').$userId);
    if(!empty($info)) {
        if ($info['parent_token'] == $token) {
            return true;
        } else {
            return false;
        }
    }

}

//生成用户登录的user_token
function makeToken($userId,$password){
    $token=md5($userId.$password.time());
    return $token;
}

//获取用户信息
function getUserInfo(){
    $userId=input('userId',0,'int');
    $redis=getRedis();
    $userInfo=$redis->get(config('user_flag').$userId);
    return $userInfo;
}

//提交频率控制
function checkSubmit($userId,$action,$intervalTime=5){
    $redis=getRedis();
    $str='time_'.$action.$userId;
    $theTime=(int)$redis->get($str);
    if(!empty($theTime)) {
        if (time() - $theTime < $intervalTime) {
            renderInfo(400008, '操作太频繁,请稍后重试');
        }
    }
    $redis->set($str,time());

}

//遍历一段日期的函数
function loopDate($startDate,$endDate){
    $start_time=strtotime($startDate);
    $end_time=strtotime($endDate);
    $data=array();
    for($i=$start_time;$i<=$end_time;$i+=60*60*24){
        //echo $i.':'.date('Y-m-d',$i).'<br />';
        $data[]=date('Y-m-d',$i);
    }
    return $data;
}

// curl header body data
function httpRequest($sUrl,$aData){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $sUrl);
    // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($aData));
    $sResult = curl_exec($ch);
    if($sError=curl_error($ch)){
        die($sError);
    }
    curl_close($ch);
    return $sResult;
}
//判断是否是手机号码
function isPhone($phone){
    if(preg_match("/^1[34578]{1}\d{9}$/",$phone)){
        return true;
    }else{
        return false;
    }
}

//php curl发送json字符串
function post_json_data($url, $data_string) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($data_string),
            'Authorization:a9cde0a2-6606-11e7-8f00-525400deb21e',
            'Accept:application/json'
        )
    );

    $rs=curl_exec($ch);

    if($sError=curl_error($ch)){
        die($sError);
    }
    curl_close($ch);
    return json_decode($rs,true);
}
//获取毫秒时间戳
function msectime()
{
    list($msec, $sec) = explode(' ', microtime());
    return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
}
//laravel自定义打印数组
function de($info){
    echo '<pre>';
    print_r($info);
    echo '</pre>';
}









